package vn.aptech.scrumgroup.ui;

import java.util.ArrayList;
import java.util.List;

import vn.aptech.scrumgroup.R;
import vn.aptech.scrumgroup.activity.ArtifactsActivity;
import vn.aptech.scrumgroup.activity.ConceptActivity;
import vn.aptech.scrumgroup.activity.EventActivity;
import vn.aptech.scrumgroup.activity.ScrumTeamActivity;
import vn.aptech.scrumgroup.model.AbstractBaseModel;
import vn.aptech.scrumgroup.model.ArrowOnCircleModel;
import vn.aptech.scrumgroup.model.CircleOnCircleModel;
import vn.aptech.scrumgroup.model.CircleWithTextModel;
import vn.aptech.scrumgroup.model.OnCircle;
import vn.aptech.scrumgroup.model.TextOnArcModel;
import vn.aptech.scrumgroup.resource.Resources;
import vn.aptech.scrumgroup.util.MenuUtil;
import vn.aptech.scrumgroup.util.TouchUtil;
import vn.aptech.scrumgroup.util.TouchingMode;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import demo.axitdn.event.TouchListener;
import demo.axitdn.physic.Unit;

public class ScrumView extends View{

	List<OnCircle> objectOnCircleList;
	List<TextOnArcModel> textOnArcList;
	List<AbstractBaseModel> modelList;
	
	CircleWithTextModel middleCircle;
	CircleWithTextModel innerCircle;
	CircleWithTextModel outerCircle;
	CircleWithTextModel ruleCircle;
	
	CircleOnCircleModel scrumTeamCircle;
	CircleOnCircleModel scrumTeamCircleInner;
	CircleOnCircleModel artifactCircle;
	CircleOnCircleModel eventCircle;
	
	TextOnArcModel inspectTxt;
	TextOnArcModel adaptTxt;
	TextOnArcModel transparencyTxt;
	TextOnArcModel ruleTxt1, ruleTxt2, ruleTxt3;
	
	ArrowOnCircleModel arrow1;
	ArrowOnCircleModel arrow2;
	ArrowOnCircleModel arrow3;
	MenuUtil menu;
	
	DisplayMetrics displaymetrics = new DisplayMetrics();
	private float x = 0f;
	private float y = 0f;
	Bundle transBundle;
	Bundle inspBundle;
	Bundle adapBundle;
	Context context;
	Intent intent;
	
	public ScrumView(Context context, int screenWidth, int screenHeight)
	{
		super(context);
		this.context = context;
		x = screenWidth / 2;
		y = screenHeight / 2;
		modelList = new ArrayList<AbstractBaseModel>();
		objectOnCircleList = new ArrayList<OnCircle>();
		textOnArcList = new ArrayList<TextOnArcModel>();
		init();
	}

	public void init(){		
		intent = new Intent();
		menu = new MenuUtil(context);
		modelList.add(menu);
		
		outerCircle = new CircleWithTextModel(x, y, 137*Unit.i, false, 0, "", 15*Unit.i, Color.rgb(139, 109, 106), Color.WHITE);
		modelList.add(outerCircle);
		ruleCircle = new CircleWithTextModel(x, y, 120*Unit.i, false, 0, "", 15*Unit.i, Color.WHITE, Color.WHITE);
		modelList.add(ruleCircle);
		scrumTeamCircle = new CircleOnCircleModel(ruleCircle, 6*Unit.i, (float)-Math.PI/2, 37*Unit.i, false, 0,this.getContext().getString(R.string.SCRUM_TEAM), 5*Unit.i, Color.rgb(164, 255, 255), Color.BLACK, Color.rgb(107, 140, 36));
		scrumTeamCircle.setContext(this.getContext());
		scrumTeamCircle.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(scrumTeamCircle.getPressState()){
					scrumTeamCircle.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						intent.setClass(context, ScrumTeamActivity.class);
						context.startActivity(intent);
					}
					scrumTeamCircle.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(scrumTeamCircle.isPointOf(x, y)){
					scrumTeamCircle.isBrilliant = true;
					this.x = x;
					this.y = y;
					scrumTeamCircle.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				if(scrumTeamCircle.getPressState()){
					scrumTeamCircle.moveTo(x, y);
				}
				return true;
			}
		});
		modelList.add(scrumTeamCircle);
		artifactCircle = new CircleOnCircleModel(ruleCircle, 6*Unit.i, (float)Math.PI/6, 37*Unit.i, false, 0, this.getContext().getString(R.string.Artifacts), 5*Unit.i, Color.rgb(164, 255, 255), Color.BLACK, Color.rgb(107, 140, 36));
		artifactCircle.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(artifactCircle.getPressState()){
					artifactCircle.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						intent.setClass(context, ArtifactsActivity.class);
						context.startActivity(intent);
					}
					artifactCircle.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(artifactCircle.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					artifactCircle.isBrilliant = true;
					artifactCircle.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				if(artifactCircle.getPressState()){
					artifactCircle.moveTo(x, y);
				}
				return false;
			}
		});
		modelList.add(artifactCircle);
		
		eventCircle = new CircleOnCircleModel(ruleCircle, 6*Unit.i, (float)Math.PI*5/6, 37*Unit.i, false, 0, this.getContext().getString(R.string.EVENT), 5*Unit.i, Color.rgb(164, 255, 255), Color.BLACK, Color.rgb(107, 140, 36));
		eventCircle.setContext(this.getContext());
		eventCircle.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(eventCircle.getPressState()){
					eventCircle.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						intent.setClass(context, EventActivity.class);
						context.startActivity(intent);
					}
					eventCircle.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(eventCircle.isPointOf(x, y)){
					eventCircle.isBrilliant = true;
					this.x = x;
					this.y = y;
					eventCircle.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				if(eventCircle.getPressState()){
					eventCircle.moveTo(x, y);
				}
				return false;
			}
		});
		modelList.add(eventCircle);
		ruleTxt1 = new TextOnArcModel(ruleCircle, outerCircle, 1*Unit.i, (float)-Math.PI/6, this.getContext().getString(R.string.Rules), Color.WHITE);
		modelList.add(ruleTxt1);
	    ruleTxt2 = new TextOnArcModel(ruleCircle, outerCircle, 1*Unit.i, (float)Math.PI/2, this.getContext().getString(R.string.Rules), Color.WHITE);
	    modelList.add(ruleTxt2);
		ruleTxt3 = new TextOnArcModel(ruleCircle, outerCircle, 1*Unit.i, (float)-Math.PI*5/6, this.getContext().getString(R.string.Rules), Color.WHITE);
		modelList.add(ruleTxt3);

		//inner round
		middleCircle = new CircleWithTextModel(x, y, 60*Unit.i, false, 0, "", 5*Unit.i, Color.rgb(139, 109, 106), Color.BLACK);
		modelList.add(middleCircle);
		
	    innerCircle = new CircleWithTextModel(x, y, 40*Unit.i,  false, 0, this.getContext().getString(R.string.SCRUM), 7*Unit.i, Color.RED, Color.WHITE);
		modelList.add(innerCircle);
		
		inspectTxt = new TextOnArcModel(innerCircle, middleCircle, 1*Unit.i, (float)-Math.PI/6, "Inspection", Color.WHITE);
		inspectTxt.setContext(this.getContext());
		
		inspectTxt.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(inspectTxt.getPressState()){
					inspectTxt.setBrilliant(false);
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle inspectBundle = new Bundle();
						inspectBundle.putString("resourceId", Resources.INSPECTION);
						inspectBundle.putString("title", context.getResources().getString(R.string.INSPECTION));
						intent.replaceExtras(inspectBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					inspectTxt.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(inspectTxt.isPointOf(x, y)){
					inspectTxt.setBrilliant(true);
					this.x = x;
					this.y = y;
					inspectTxt.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		modelList.add(inspectTxt);
		
		adaptTxt = new TextOnArcModel(innerCircle, middleCircle, 1*Unit.i, (float)Math.PI/2, "Adaptation", Color.WHITE);
		adaptTxt.setContext(this.getContext());
		adaptTxt.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(adaptTxt.getPressState()){
					adaptTxt.setBrilliant(false);
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle adaptBundle = new Bundle();
						adaptBundle.putString("resourceId", Resources.ADAPTATION);
						adaptBundle.putString("title", context.getResources().getString(R.string.ADAPTATION));
						intent.replaceExtras(adaptBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					adaptTxt.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(adaptTxt.isPointOf(x, y)){
					adaptTxt.setBrilliant(true);
					this.x = x;
					this.y = y;
					adaptTxt.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});

		modelList.add(adaptTxt);
		transparencyTxt = new TextOnArcModel(innerCircle, middleCircle, 1*Unit.i, (float)-Math.PI*5/6, "Transparency", Color.WHITE);
		transparencyTxt.setContext(this.getContext());
		transparencyTxt.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(transparencyTxt.getPressState()){
					transparencyTxt.setBrilliant(false);
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle transBundle = new Bundle();
						transBundle.putString("resourceId", Resources.TRANSPARENCY);
						transBundle.putString("title", context.getResources().getString(R.string.TRANSPARENCY));
						intent.replaceExtras(transBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					transparencyTxt.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(transparencyTxt.isPointOf(x, y)){
					transparencyTxt.setBrilliant(true);
					this.x = x;
					this.y = y;
					transparencyTxt.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		modelList.add(transparencyTxt);
		
		arrow1 = new ArrowOnCircleModel(middleCircle, scrumTeamCircle, Color.rgb(231, 60, 10));
		modelList.add(arrow1);
		arrow2 = new ArrowOnCircleModel(middleCircle, artifactCircle, Color.rgb(231, 60, 10));
		modelList.add(arrow2);
		arrow3 = new ArrowOnCircleModel(middleCircle, eventCircle, Color.rgb(231, 60, 10));
		modelList.add(arrow3);	

		//observers for round on circle motion 
		objectOnCircleList.add(scrumTeamCircle);
		objectOnCircleList.add(artifactCircle);
		objectOnCircleList.add(eventCircle);
		objectOnCircleList.add(ruleTxt1);
		objectOnCircleList.add(ruleTxt2);
		objectOnCircleList.add(ruleTxt3);
		objectOnCircleList.add(transparencyTxt);
		objectOnCircleList.add(adaptTxt);
		objectOnCircleList.add(inspectTxt);
		
		for (OnCircle model:objectOnCircleList){
			for (int i=0;i<objectOnCircleList.size();i++)
				if (model!=objectOnCircleList.get(i))
					model.addMoveRoundListener(objectOnCircleList.get(i));
		}
		for (AbstractBaseModel model:modelList)
			model.setContext(this.getContext());
	}//end init
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawColor(Color.WHITE);
		//anti alias and draw all
		for (AbstractBaseModel model: modelList){
			model.onDraw(canvas);//end if
		}//end for	
	}//end onDraw
	
//	TouchingMode mode = TouchingMode.NONE;
//	float oldDist = 0.0f;
//	float oldUnitI = Unit.i;
//	float oldUnitJ = Unit.j;
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
//		switch (event.getAction() & MotionEvent.ACTION_MASK) {
//	        case MotionEvent.ACTION_POINTER_DOWN:
//	            oldDist = TouchUtil.spacing(event);
//	            if (oldDist>10)
//	            	mode = TouchingMode.ZOOM;
//	            break;
//	        case MotionEvent.ACTION_MOVE:
//	        	if (mode==TouchingMode.ZOOM){
//	        		float newDist = TouchUtil.spacing(event);
//	        		if (newDist>10){
//	        			float scale = newDist/oldDist;
//	        			Unit.i = oldUnitI*scale;
//	        			Unit.j = oldUnitJ*scale;
//	        		}//end if
//	        	}
//	        	break;
//	        case MotionEvent.ACTION_POINTER_UP:
//                mode = TouchingMode.NONE;
//                break;	
//	        default:
//	        	break;
//		}//end switch
		for(AbstractBaseModel model : modelList){
			model.fireTouchEvent(event);
		}
		this.invalidate();
		return true;
	}//end onTouch
}
